$(function () {
    //地址是：<span id="pro_city"></span><input id="txtAdd" type="text" />
    // var address = $("#pro_city");
    var province = $("#province");
    var city = $("#city");
    var area = $("#area");
    var street = $("#street");
    var preProvince = "<option value=\"\">选择省（市）</option>";
    var preCity = "<option value=\"\">选择市</option>";
    var preArea = "<option value=\"\">选择区（县）</option>";
    var preStreet = "<option value=\"\">选择街道</option>";
    var hkaddress=$("#hid_hukouaddress").val();
    var addressArray;
    if(!!hkaddress){
        addressArray=hkaddress.split('_');
    }
    province.html(preProvince);
    city.html(preCity);
    area.html(preArea);
    street.html(preStreet);
    var addressXml;
    $.ajax({
        type: "GET",
        url: "../../resources/js/plugins/province/Area.xml",
        success: func_suc_getXmlProvice
    });
    province.change(function () {
        if (province.val() != "") {
            city.html(preCity);
            func_suc_getXmlCity(addressXml);
            city.change();
        }
    });
    city.change(function () {
        if (city.val() != "") {
            area.html(preArea);
            func_suc_getXmlArea(addressXml);
            area.change();
        }
    });
    area.change(function () {
        if (area.val() != "") {
            street.html(preStreet);
            func_suc_getXmlStreet(addressXml);
        }
    });

    function func_suc_getXmlProvice(xml) {
        addressXml=xml;
        var sheng = $(xml).find("province");
        sheng.each(function (i) {
            if(!!addressArray&&addressArray.length>0&&sheng.eq(i).attr("name")==addressArray[0]){
                province.append("<option value=" + i + " selected>" + sheng.eq(i).attr("name") + "</option>");
                func_suc_getXmlCity(addressXml);
            }else{
                province.append("<option value=" + i + ">" + sheng.eq(i).attr("name") + "</option>");
            }

        });

    }
    function func_suc_getXmlCity(xml) {
        var xml_sheng = $(xml).find("province");
        var pro_num = parseInt(province.val());
        var xml_shi = xml_sheng.eq(pro_num).find("city");
        xml_shi.each(function (j) {
            if(!!addressArray&&addressArray.length>0&&xml_shi.eq(j).attr("name")==addressArray[1]){
                city.append("<option  value=" + j + " selected>" + xml_shi.eq(j).attr("name") + "</option>");
                func_suc_getXmlArea(addressXml);
            }else{
                city.append("<option  value=" + j + ">" + xml_shi.eq(j).attr("name") + "</option>");
            }

        });
    }
    function func_suc_getXmlArea(xml) {
        var xml_sheng = $(xml).find("province");
        var pro_num = parseInt(province.val());
        var xml_shi = xml_sheng.eq(pro_num).find("city");
        var city_num = parseInt(city.val());
        var xml_xianqu = xml_shi.eq(city_num).find("country");
        xml_xianqu.each(function (k) {
            if(!!addressArray&&addressArray.length>0&&xml_xianqu.eq(k).attr("name")==addressArray[2]){
                area.append("<option  value=" + k + " selected>" + xml_xianqu.eq(k).attr("name") + "</option>");
                $("#txt_street").val(addressArray[3]);
            }else{
                area.append("<option  value=" + k + ">" + xml_xianqu.eq(k).attr("name") + "</option>");
            }
        });
    }

    function func_suc_getXmlStreet(xml) {
        var xml_sheng = $(xml).find("province");
        var pro_num = parseInt(province.val());
        var xml_shi = xml_sheng.eq(pro_num).find("city");
        var city_num = parseInt(city.val());
        var xml_xianqu = xml_shi.eq(city_num).find("country");
        var area_num = parseInt(area.val());
        var xml_jiedao = xml_xianqu.eq(area_num).find("street");
        xml_jiedao.each(function (k) {
            street.append("<option  value=" + k + ">" + xml_jiedao.eq(k).attr("name") + "</option>");
        });
    }
});